﻿using StockTools.ViewModel;
using System.Windows.Controls;
using System.Windows.Input;

namespace StockTools.Modules
{
    /// <summary>
    /// Interaction logic for CostCalculation
    /// </summary>
    public partial class CostCalculation : UserControl
    {
        CostCalculationViewModel costCalculationViewModel = new CostCalculationViewModel();
        public CostCalculation()
        {
            InitializeComponent();

            this.DataContext = costCalculationViewModel;

            costCalculationViewModel.getStockBaseInfo();

            //tbBuyingCode.TextChanged += TextBox_TextChanged;
        }
        private void ConsultClick_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var money = (sender as TextBlock).Text.Replace("¥", "");
            decimal res;
            if (decimal.TryParse(money, out res))
            {
                costCalculationViewModel.SellingResult.SellPrice = res;
            }
            else
            {

            }
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            var txt = (sender as ComboBox).Text.Trim();
            if (txt.Length < 6) return;

            costCalculationViewModel.getStockBaseInfo(txt).Wait();
        }

        private void PackIcon_MouseDown(object sender, MouseButtonEventArgs e)
        {
            costCalculationViewModel.getStockBaseInfo().Wait();
        }
        private void StockInfo_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var exchangeCode = costCalculationViewModel.StockInfo.Code.StartsWith("6") ? "sh" : "sz";
            var url = $"http://quote.eastmoney.com/{exchangeCode}{costCalculationViewModel.StockInfo.Code}.html";
            System.Diagnostics.Process.Start("explorer.exe", url);
        }

        private void TextBlock_DragLeave(object sender, System.Windows.DragEventArgs e)
        {
            e.Data.SetData(((TextBlock)sender).Text);
        }
    }
}
